﻿using System;

using ARCHFORME.Common;
using ARCHFORME.Core;

using FakeItEasy;

using FluentAssertions;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Core.Tests
{
    [TestClass]
    public class CommandServicesSpecs
    {
        [TestMethod]
        public void When_given_a_command_it_should_find_a_handler_to_excute_command()
        {
            /********* Arrange ********************/
            var command = A.Fake<ICommand>();
            var handler = A.Fake<IHandleCommand<ICommand>>();
            CommandService commandService = new CommandServiceBuilder().Register(command, handler).Build();

            /********* Act     ********************/
            commandService.Execute(command);

            /********* Assert  ********************/
            A.CallTo(() => handler.Handle(command)).MustHaveHappened();
        }

        [TestMethod]
        public void When_given_a_command_for_which_no_handler_can_be_found_it_should_throw()
        {
            /********* Arrange ********************/
            var command = A.Fake<ICommand>();
            CommandService commandService = new CommandServiceBuilder().Build();

            /********* Act     ********************/
            Action action = () => commandService.Execute(command);

            /********* Assert  ********************/
            action.ShouldThrow<NoHandlerRegisteredForCommandException>().WithMessage("For command: " + command.GetType().Name);
        }



    }
}